import {SEARCH} from '../api'
export default (state,action)=>{
  state = state || {hot:['周杰伦','华语原创十大金曲','陪你到世界之巅','SOMI','17岁','想你的夜'],currentNumber:20,currentPage:0,totalNumber:0,songList:[]};
  switch(action.type){
    case 'hot':
      return {hot:''}
    case 'search_result':
      return {...state,currentNumber:action.currentNumber,currentPage:action.currentPage,totalNumber:action.totalNumber,songList:state.songList.concat(...action.songList)}
    case 'clearList':
      return {...state,songList:[]}
    case 'load_more':
      return {...state,currentPage:++state.currentPage}
      // return Object.assign({},state,{currentPage:state.currentPage+1})
    default:
      return state;
  }
}

export const clearList = () => (dispatch) => {
  dispatch({type:'clearList'})
}
export const searchList = (text,page) => (dispatch) => {
  fetch(SEARCH+text+'/'+(page+1)).then(response=>response.json()).then(result=>{
    if(result.errno===0){
      dispatch({type:'search_result',currentNumber:result.data.page.currentNumber,
      currentPage:result.data.page.currentPage,totalNumber:result.data.page.totalNumber,songList:result.data.songList})
    }
  })
}